#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
#endregion

namespace Thn.Data.Persistence
{
    /// <summary>
    /// Base abstract class for persistent object
    /// </summary>
    public abstract class PersistentObject : IPersistentObject
    {
        #region Get Identifier
        /// <summary>
        /// Retrieves the identifier of this object
        /// </summary>
        protected abstract object OnGetIdentifier();

        /// <summary>
        /// Retrieves the identifier of this object
        /// </summary>
        object IPersistentObject.GetIdentifier()
        {
            return OnGetIdentifier();
        }
        #endregion

        #region Persistent
        private bool mPersistent = false;
        /// <summary>
        /// Gets whether this object has been persisted to database
        /// </summary>
        public bool Persistent
        {
            get { return mPersistent; }
        }

        /// <summary>
        /// Sets value for Persistent flag
        /// </summary>
        internal void SetPersistentFlag(bool value)
        {
            mPersistent = value;
        }
        #endregion
	
        #region Modified
        private bool mModified = false;
        /// <summary>
        /// Gets whether this object has been changed
        /// </summary>
        public bool Modified
        {
            get { return mModified; }
        }

        /// <summary>
        /// Sets value for Modified flag
        /// </summary>
        internal void SetModifiedFlag(bool value)
        {
            mModified = value;
        }
        #endregion

        #region Context
        private IPersistentContext mContext;
        /// <summary>
        /// Gets/Sets the persistent context this object belongs to.
        /// <para>At any time, a persistent object is tracked by one context only.</para>
        /// <para>If context is null, this object is detached from any persistence layer</para>
        /// </summary>
        public IPersistentContext Context
        {
            get { return mContext; }
            set { mContext = value; }
        }        
        #endregion

        #region Save State
        ///// <summary>
        ///// Save a snapshot of this object's values
        ///// </summary>
        //public object SaveState()
        //{
        //    return OnSaveState();
        //}

        ///// <summary>
        ///// Save a snapshot of this object's values
        ///// </summary>
        //protected virtual object OnSaveState()
        //{
        //    return null;
        //}
        #endregion

        #region Load State
        ///// <summary>
        ///// Load values for this object from a state bag
        ///// </summary>
        //public void LoadState(object state)
        //{
        //    OnLoadState(state);
        //}

        ///// <summary>
        ///// Load values for this object from a state bag
        ///// </summary>
        //protected virtual void OnLoadState(object state)
        //{
        //}
        #endregion

        #region Property Changed
        bool mSuppressPropertyChanged = false;

        /// <summary>
        /// Calls this method when a property of this object is changed to set modification state
        /// </summary>
        /// <param name="propertyName">Name of the property that has been changed</param>
        /// <param name="oldValue">previous value of the property</param>
        /// <param name="newValue">new value of the property</param>
        protected void PropertyChanged(string propertyName, object oldValue, object newValue)
        {
            if (!mSuppressPropertyChanged)
            {
                mModified = true;
                OnPropertyChanged(propertyName, oldValue, newValue);
                if (Changed != null) Changed(this, propertyName, oldValue, newValue);
            }
        }

        /// <summary>
        /// This method is raised when a property is changed.
        /// </summary>
        /// <param name="propertyName">Name of the property that has been changed</param>
        /// <param name="oldValue">previous value of the property</param>
        /// <param name="newValue">new value of the property</param>
        protected virtual void OnPropertyChanged(string propertyName, object oldValue, object newValue)
        {
            if ((mContext != null) && (mContext is PersistentContext))
            {
                ((PersistentContext)mContext).DoPropertyChanged(this, propertyName, oldValue, newValue);
            }
        }

        /// <summary>
        /// This event is raised when a property of this object is changed
        /// </summary>
        public event PropertyChangedHandler Changed = null;
        #endregion

        #region Suppress Property Changed
        /// <summary>
        /// Temporarily cancel the PropertyChanged() method
        /// </summary>
        public void SuppressPropertyChanged()
        {
            mSuppressPropertyChanged = true;
        }
        #endregion

        #region Resume Property Changed
        /// <summary>
        /// Resume the effect of PropertyChanged() method
        /// </summary>
        public void ResumePropertyChanged()
        {
            mSuppressPropertyChanged = false;
        }
        #endregion

        #region Equals
        /// <summary>
        /// Override base.Equals so that any persistent object with the same Identifier is considered equal
        /// </summary>
        public override bool Equals(object obj)
        {
            bool result = false;

            if (obj is IPersistentObject)
            {
                IPersistentObject target = (IPersistentObject)obj;
                object targetID = target.GetIdentifier();
                object myID = this.OnGetIdentifier();

                if ((myID is IComparable) && (targetID != null) && (myID.GetType().Equals(targetID.GetType()))) result = ((IComparable)myID).CompareTo(targetID) == 0;
                else result = targetID == myID;
            }
            else result = base.Equals(obj);

            return result;
        }
        #endregion
    }

    /// <summary>
    /// Delegate for event Changed of persistent objects
    /// </summary>
    /// <param name="sender">The persistent object that raised this event</param>
    /// <param name="propertyName">Name of the property that has been changed</param>
    /// <param name="oldValue">previous value of the property</param>
    /// <param name="newValue">new value of the property</param>
    public delegate void PropertyChangedHandler(PersistentObject sender, string propertyName, object oldValue, object newValue);
}
